# encoding: utf-8
""" 
@version: v1.0 
@author: autumner 
@license: Apache Licence  
@contact: 18322313385@163.com 
@site:  
@software: PyCharm 
@file: decorator_class1.py 
@time: 2019/2/25 11:47
"""
'''
使用闭包和nonlocal变量实现的装饰器
'''
import types
from functools import wraps

def profiled(func):
    ncalls = 0
    @wraps(func)
    def wrapper(*args, **kwargs):
        nonlocal ncalls
        ncalls += 1
        return func(*args, **kwargs)
    wrapper.ncalls = lambda: ncalls
    return wrapper

@profiled
def add(x, y):
    return x + y

print(add(2, 3))
print(add(4, 5))
print(add.ncalls())